<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;
$ignoreAuth = true;

if (!isset($_REQUEST['medico']))
    session_start();

$landingpage = "index.php?site=" . $_SESSION['site_id'];
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    if (!isset($_REQUEST['medico'])) {
        session_destroy();
        header('Location: ' . $landingpage . '&w');
		echo "heres the error222";
        exit;
    }
}

include_once('../interface/globals.php');
require_once("$srcdir/patient_provider.inc.php");

function is_actual_encounter(){
    if ( $_SESSION['encounter'] > 0 ){
        $query = " SELECT * "
                . " FROM form_encounter "
                . " WHERE encounter = " .$_SESSION['encounter'] . " AND pid = " . $_SESSION['pid']. " ";
        $result = sqlStatement($query);
        if (sqlNumRows($result) > 0 ){
            $row = sqlFetchArray($result);
            if( $row['closed'] == 0 ){
                return true;
            }else{
                return false;
            }
        }else{
            return false;    
        }
    }else{
        return false;
    }
}

if (isset($_REQUEST['medico']))
    $pid = $_REQUEST['set_pid'];
$medico = "";
$medicoIni = "";
if (isset($_REQUEST['medico'])) {
    $medico = "&medico=1&set_pid=" . $pid;
    $medicoIni = "?medico=1&set_pid=" . $pid;
}
?>
<div id='vitals' style='padding-bottom: 5px'><!--outer div-->
    <?php
//retrieve most recent set of vitals.
    $result = sqlQuery("SELECT FORM_VITALS.date, FORM_VITALS.id FROM form_vitals AS FORM_VITALS LEFT JOIN forms AS FORMS ON FORM_VITALS.id = FORMS.form_id WHERE FORM_VITALS.pid=? AND FORMS.deleted != '1' ORDER BY FORM_VITALS.date DESC", array($pid));

    if (!$result) { //If there are no disclosures recorded
        echo "<h2>No se han ingreso signos vitales.</h2>";
        $vall = is_actual_encounter();
        if ( $vall ){
            echo "<a class='addIcon css_button css_button_left iframe btnNav fancybox.iframe' href='" . $web_root . "/patients/load_form.php?formname=vitals" . $medico . "' title='Agregar' id='agregar_vitales'>
            <i class='icon-plus-sign'></i></a>";
        }
         
    } else {
        echo "<h2>Ultima toma <a class='addIcon css_button css_button_left iframe btnNav fancybox.iframe' href='" . $web_root . "/patients/load_form.php?formname=vitals" . $medico . "' title='Agregar' id='agregar_vitales'>
            <i class='icon-plus-sign'></i></a>
            <span style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 5px;'> " . toDateView($result["date"]) . " </span></h2>";
        include_once("report_vitals.php");
        call_user_func("vitals_report", '', '', 2, $result['id']);
        ?>
        <a href='<?php echo $web_root; ?>/patients/get_vitals_grafico.php?formname=vitals&view=0<?php echo $medico; ?>' style="margin-left: 5px" class="enlace_grafico iframe_grafico btnNav fancybox.iframe" id='grafico'>Detalle M&eacute;dico</a>           
        <?php
    }
    ?>
</div>

<script type="text/javascript" language="JavaScript">
    $(document).ready(function() {
        $('#grafico').click(function(e) {
            e.preventDefault();
            parent.$.fancybox({
                href: this.href,
                autoSize: false,
                type: 'iframe',
                iframe: {
                    preload: false // fixes issue with iframe and IE
                },
                helpers: {
                    overlay: {
                        opacity: 0.3,
                        css: {
                            'background': 'rgba(255, 254, 254,0.85)'
                        }
                    } // overlay
                }// helpers
            }); // fancybox
        }); // click
        $('#grafico2').click(function(e) {
            e.preventDefault();
            parent.$.fancybox({
                href: this.href,
                autoSize: false,
                type: 'iframe',
                iframe: {
                    preload: false // fixes issue with iframe and IE
                },
                helpers: {
                    overlay: {
                        opacity: 0.3,
                        css: {
                            'background': 'rgba(255, 254, 254,0.85)'
                        }
                    } // overlay
                }
            }); // fancybox
        }); // click
        $('#agregar_vitales').click(function(e) {
            e.preventDefault();
            parent.$.fancybox({
                href: this.href,
                maxWidth: 920,
                maxHeight: 1800,
                autoSize: true,
                type: 'iframe',
                iframe: {
                    preload: false // fixes issue with iframe and IE
                },
                helpers: {
                    overlay: {
                        opacity: 0.3,
                        css: {
                            'background': 'rgba(255, 254, 254,0.85)',
                            'overflow': 'hidden'
                        }
                    } // overlay
                }, // helpers
                afterClose: function() {
                    $("#signos").load("<?php echo $web_root; ?>/patients/get_vitals_fragment.php<?php echo $medicoIni; ?>");
                    $("#consulta_medica").load("<?php echo $web_root?>/interface/patient_file/encounter/forms.php?set_encounter=<?php echo $_SESSION['encounter'];?>");

                }//afterClose
            }); // fancybox
        }); // click
    });
    /********************************************************/
</script>
<!--[if lt IE 7]>
<style type='text/css'>
        #simplemodal-container a.modalCloseImg {
                background:none;
                right:-14px;
                width:22px;
                height:26px;
                filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(
                        src='img/x.png', sizingMethod='scale'
                );
        }
</style>
<![endif]-->